/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Jul 30, 2008

package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.jme.renderer.Renderer;
import com.jme.renderer.pass.Pass;
import com.jme.scene.PassNodeState;
import com.jme.scene.Spatial;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.ClipState;
import com.jme.scene.state.ColorMaskState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.FragmentProgramState;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.ShadeState;
import com.jme.scene.state.StencilState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.VertexProgramState;
import com.jme.scene.state.WireframeState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.SwingComponentFactory;

public class NewRenderStatePanel extends JPanel {

	private		JDialog		mcl_owner;
	private		Object		mcl_parent;
	
	public
	NewRenderStatePanel(Object cl_parent, JDialog cl_owner) {
		super();
		
		mcl_owner = cl_owner;
		mcl_parent = cl_parent;
		
		build();
	}
	
	private	void
	build() {
		final	JComboBox		lcl_combo = SwingComponentFactory.makeAComboBox();
		add(lcl_combo);
		final	Renderer		lcl_render = DisplaySystem.getDisplaySystem().getRenderer();
		
		lcl_combo.addItem(new RenderStateEntry(BlendState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createBlendState();
			}
		}));
		
		lcl_combo.addItem(new RenderStateEntry(ClipState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createClipState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(ColorMaskState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createColorMaskState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(CullState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createCullState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(FogState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createFogState();
				
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(FragmentProgramState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createFragmentProgramState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(GLSLShaderObjectsState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createGLSLShaderObjectsState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(LightState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createLightState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(MaterialState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createMaterialState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(ShadeState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createShadeState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(StencilState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createStencilState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(TextureState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createTextureState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(VertexProgramState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createVertexProgramState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(WireframeState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createWireframeState();
			}
			
		}));
		
		lcl_combo.addItem(new RenderStateEntry(ZBufferState.class, new CreateRenderStateFunc() {

			public RenderState createRenderState() {
				return lcl_render.createZBufferState();
			}
			
		}));
		
		JButton		lcl_addState = SwingComponentFactory.makeAButton("Add State");
		lcl_addState.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				final	RenderStateEntry		lcl_e = (RenderStateEntry)lcl_combo.getSelectedItem();
				
				if(lcl_e == null) { return; }
				
				GameTaskQueueManager.getManager().update(new Callable<Object>() {

					public Object call() throws Exception {
						RenderState		lcl_rs = null;
						lcl_rs = lcl_e.mcl_createRSFunc.createRenderState();
						
						if(lcl_rs == null) { return null; }
						
						lcl_rs.setEnabled(true);
						
						if(mcl_parent instanceof Spatial) {
							((Spatial)mcl_parent).setRenderState(lcl_rs);
							((Spatial)mcl_parent).updateRenderState();
						} else if(mcl_parent instanceof Pass) {
							((Pass)mcl_parent).setPassState(lcl_rs);
						} else if(mcl_parent instanceof PassNodeState) {
							((PassNodeState)mcl_parent).setPassState(lcl_rs);
						}
						
						return null;
					}
					
				});
			}
			
		});
		add(lcl_addState);
		
		JButton		lcl_done = SwingComponentFactory.makeAButton("Done");
		lcl_done.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				mcl_owner.setVisible(false);
			}
			
		});
		add(lcl_done);
	}
	
	private	interface	CreateRenderStateFunc {
		RenderState		createRenderState();
	}
	
	private	class	RenderStateEntry {
		public	Class						mcl_rsClass;
		public	CreateRenderStateFunc		mcl_createRSFunc;
		
		public
		RenderStateEntry(Class cl_class, CreateRenderStateFunc cl_func) {
			mcl_rsClass = cl_class;
			setCreateRenderStateFunc(cl_func);
		}
		
		@Override
		public String toString() {
			return mcl_rsClass.getSimpleName();
		}
		
		public	void
		setCreateRenderStateFunc(CreateRenderStateFunc cl_func) {
			mcl_createRSFunc = cl_func;
		}
		
	}
}
